# -*- coding: utf-8 -*-

from __future__ import division
import pygame
import Entity
import pygame.gfxdraw
from pygame.locals import *
from utility import *

class Field(Entity.Field):
  margin     = 3    # mètres

  def __init__(self):
    Entity.Field.__init__(self, 105, 68)

  def initZones(self):
    Entity.Field.initZones(self)
    self.dmainBorderRect = self.mainBorderRect
    self.dpenaltyArea1   = self.penaltyArea1
    self.dpenaltyArea2   = self.penaltyArea2
    self.dendZone1       = self.endZone1
    self.dendZone2       = self.endZone2
    self.dgoal1Rect      = self.goal1Rect
    self.dgoal2Rect      = self.goal2Rect
    self.dpenalty1Circle = self.penalty1Circle
    self.dpenalty2Circle = self.penalty2Circle
    self.dmiddleCircle   = self.middleCircle

  def initGraphics(self, g):
    self.g = g
    self.screenWidth  = self.g.get_size()[0]
    self.screenHeight = self.g.get_size()[1]
    screenRatio = self.screenHeight / self.screenWidth
    fieldRatio  = self.width / self.length
    if screenRatio <= fieldRatio:
      fw = self.screenHeight - 2 * self.margin * self.detail
      fl = fw / fieldRatio
      x = (self.screenWidth - fl) / 2
      y = self.margin * self.detail
    else:
      fl = self.screenWidth - 2 * self.margin * self.detail
      fw = fl * fieldRatio
      x = self.margin * self.detail
      y = (self.screenHeight - fw) / 2
    self.dimension = (fl, fw)
    self.origin    = (x, y)
    self.ratio     = fw / self.width
    self.dmainBorderRect = self.__transformRect(self.mainBorderRect  )
    self.dpenaltyArea1   = self.__transformRect(self.penaltyArea1    )
    self.dpenaltyArea2   = self.__transformRect(self.penaltyArea2    )
    self.dendZone1       = self.__transformRect(self.endZone1        )
    self.dendZone2       = self.__transformRect(self.endZone2        )
    self.dgoal1Rect      = self.__transformRect(self.goal1Rect       )
    self.dgoal2Rect      = self.__transformRect(self.goal2Rect       )
    self.dpenalty1Circle = self.__transformCircle(self.penalty1Circle)
    self.dpenalty2Circle = self.__transformCircle(self.penalty2Circle)
    self.dmiddleCircle   = self.__transformCircle(self.middleCircle  )

  def draw(self):
    self.g.fill(Color.Grass)
    # corners
    pygame.gfxdraw.aacircle(self.g, int(self.origin[0]                    ), int(self.origin[1]                    ), int(self.detail), Color.White)
    pygame.gfxdraw.aacircle(self.g, int(self.origin[0] + self.dimension[0]), int(self.origin[1]                    ), int(self.detail), Color.White)
    pygame.gfxdraw.aacircle(self.g, int(self.origin[0] + self.dimension[0]), int(self.origin[1] + self.dimension[1]), int(self.detail), Color.White)
    pygame.gfxdraw.aacircle(self.g, int(self.origin[0]                    ), int(self.origin[1] + self.dimension[1]), int(self.detail), Color.White)
    pygame.gfxdraw.box(self.g, (0                                 , 0                                 , self.origin[0]   , self.screenHeight), Color.Grass)
    pygame.gfxdraw.box(self.g, (self.origin[0] + self.dimension[0], 0                                 , self.origin[0]   , self.screenHeight), Color.Grass)
    pygame.gfxdraw.box(self.g, (0                                 , 0                                 , self.screenWidth , self.origin[1]   ), Color.Grass)
    pygame.gfxdraw.box(self.g, (0                                 , self.origin[1] + self.dimension[1], self.screenWidth , self.origin[1]   ), Color.Grass)

    # countour du terrain et ligne centrale
    pygame.gfxdraw.rectangle(self.g, self.dmainBorderRect                                                                      , Color.White)
    pygame.gfxdraw.vline(self.g, int(self.origin[0] + self.dimension[0] / 2), int(self.origin[1]), int(self.origin[1] + self.dimension[1]), Color.White)

    # cercle central et arcs des 11m des buts
    pygame.gfxdraw.aacircle(self.g, self.dmiddleCircle[0][0]  , self.dmiddleCircle[0][1]  , self.dmiddleCircle[1]  , Color.White)
    pygame.gfxdraw.aacircle(self.g, self.dpenalty1Circle[0][0], self.dpenalty1Circle[0][1], self.dpenalty1Circle[1], Color.White)
    pygame.gfxdraw.aacircle(self.g, self.dpenalty2Circle[0][0], self.dpenalty2Circle[0][1], self.dpenalty2Circle[1], Color.White)
    pygame.gfxdraw.box(self.g, self.dpenaltyArea1, Color.Grass)
    pygame.gfxdraw.box(self.g, self.dpenaltyArea2, Color.Grass)
    # points de pénalities
    pygame.gfxdraw.filled_circle(self.g, self.dpenalty1Circle[0][0], self.dpenalty1Circle[0][1], 2, Color.White)
    pygame.gfxdraw.filled_circle(self.g, self.dpenalty2Circle[0][0], self.dpenalty2Circle[0][1], 2, Color.White)

    # surface de réparation, zones gardien et cages
    pygame.gfxdraw.rectangle(self.g, self.dpenaltyArea1, Color.White)
    pygame.gfxdraw.rectangle(self.g, self.dpenaltyArea2, Color.White)
    pygame.gfxdraw.rectangle(self.g, self.dendZone1    , Color.White)
    pygame.gfxdraw.rectangle(self.g, self.dendZone2    , Color.White)
    pygame.gfxdraw.rectangle(self.g, self.dgoal1Rect   , Color.White)
    pygame.gfxdraw.rectangle(self.g, self.dgoal2Rect   , Color.White)

  def __transformRect(self, rect):
    return (int(rect[0] * self.ratio + self.origin[0]), \
            int(rect[1] * self.ratio + self.origin[1]), \
            int(rect[2] * self.ratio),                  \
            int(rect[3] * self.ratio))

  def __transformCircle(self, circle):
    return ((int(circle[0][0] * self.ratio + self.origin[0]),  \
             int(circle[0][1] * self.ratio + self.origin[1])), \
             int(circle[1]    * self.ratio))

class Team(Entity.Team):
  def __init__(self, name, color):
    Entity.Team.__init__(self, name)
    self.color = color

  def initGraphics(self, g, origin, ratio):
    for p in self.players:
      p.initGraphics(g, origin, ratio)

  def draw(self):
    for p in self.players:
      p.draw(self.color)

  def setRealSize(self, b):
    for p in self.players:
      p.setRealSize(b)

class Player(Entity.Player):
  def __init__(self, name):
    Entity.Player.__init__(self, name)

  def initGraphics(self, g, origin, ratio):
    self.g      = g
    self.origin = origin
    self.ratio  = ratio
    self.ray    = self.diameter * self.ratio

  def setRealSize(self, b):
    if b:
      self.ray = int(self.diameter * self.ratio)
      if self.ray < 1: self.ray = 1
    else:
      self.ray = 6

  def draw(self, color):
    l = self.location
    centre = int(self.origin[0] + l.x * self.ratio), int(self.origin[1] + l.y * self.ratio)
    if self.ray <= 2:
      pygame.gfxdraw.filled_circle(self.g, centre[0], centre[1], self.ray, color)
      pygame.gfxdraw.aacircle(self.g, centre[0], centre[1], self.ray + 1, color)
    else:
      dv  = self.orientation.getDirectionVector().getNormalizedVector()
      vs  = dv             * self.ray
      vns = dv.getNormal() * 2 * self.ray
      point0 = Vector(centre[0], centre[1])
      point1 = point0 - vns / 2
      point2 = point1 + vns
      point3 = point0 + vs
      pygame.gfxdraw.filled_circle (self.g, centre[0], centre[1], self.ray, color)
      pygame.gfxdraw.aacircle      (self.g, centre[0], centre[1], self.ray, color)
      pygame.gfxdraw.filled_polygon(self.g, ((point1.x, point1.y), (point2.x, point2.y), (point3.x, point3.y)), Color.Black)

class Ball(Entity.Ball):
  def __init__(self, location):
    Entity.Ball.__init__(self, location)

  def initGraphics(self, g, origin, ratio):
    self.g      = g
    self.origin = origin
    self.ratio  = ratio
    self.ray    = self.diameter * self.ratio

  def setRealSize(self, b):
    if b:
      self.ray = int(self.diameter * self.ratio)
      if self.ray < 1: self.ray = 1
    else:
      self.ray = 4

  def draw(self):
    l = self.location
    centre = (int(self.origin[0] + l.x * self.ratio), int(self.origin[1] + l.y * self.ratio))
    pygame.gfxdraw.filled_circle(self.g, centre[0], centre[1], self.ray, Color.Gold)
    pygame.gfxdraw.aacircle(self.g, centre[0], centre[1], self.ray + 1, Color.Black)
  
class Game(Entity.Game):
  def __init__(self, field, team1, team2, ball, screen):
    Entity.Game.__init__(self, field, team1, team2, ball)
    self.screen = screen
    self.g      = pygame.Surface(screen.get_size())
    self.g      = self.g.convert()
    self.field.initGraphics(self.g)
    self.team1.initGraphics(self.g, self.field.origin, self.field.ratio)
    self.team2.initGraphics(self.g, self.field.origin, self.field.ratio)
    self.ball.initGraphics (self.g, self.field.origin, self.field.ratio)

  def setRealSize(self, b):
    self.team1.setRealSize(b)
    self.team2.setRealSize(b)
    self.ball.setRealSize(b)

  def draw(self):
    self.field.draw()
    self.team1.draw()
    self.team2.draw()
    self.ball.draw()
    self.screen.blit(self.g, (0, 0))
    


